﻿from jutils import *

import json

#bgtp
TCP_PORT = 1024;

#Состояния КА сервера
KA_0 = '0';
KA_WORK = 'work';
KA_GAME = 'game';

#Состояния КА игры
GM_0 = '0';
GM_SETTINGS = 'settings';
GM_GAME = 'game';
GM_GAMEOVER = 'gameover';

#
USR_GAMEMASTER = "GameMaster";
USR_GAMEMANAGER = "GameManager";
USR_SERVER = "Server";

#части сообщений
MSG_USR = "USR";
MSG_TYPE = "TYPE";
MSG_BODY = "BODY";

#ответ
ANS_TEXT = "TEXT";
ANS_ID = "ID";
ANS_VAL = "VALUE";
ANS_TYPE = "TYPE";

class Request:
	def __init__(this, msg):
		if (type(msg) == dict):
			this.usr = msg.get(MSG_USR);
			this.type = msg.get(MSG_TYPE);
			this.bodyText = msg.get(MSG_BODY, "");
		else:
			this.usr = msg[0];
			this.type = msg[1];
			this.bodyText = msg[2];
		
		if (len(this.bodyText) == 0):
			this.body = "";
		else:
			this.body = json.loads(this.bodyText);
		
		#//answer
		this.answer = null;
		this.who = null;
		this.isAns = false;
	
	def Answer(this, who_, ans_):
		if (type(ans_) != dict):
			raise Exception("answer must be the dyct")
		this.answer = ans_;
		this.who = who_.name;
		this.isAns = true;
		return this;

	def getAnswer(this):
		if (not this.isAns):
			null_ans = {};
			null_ans[ANS_ID] = null;
			null_ans[ANS_VAL] = null;			
			null_ans[ANS_TEXT] = "No such req";
			answer_ = null_ans;
		else:
			answer_ = this.answer;
		answer_[ANS_TYPE] = this.type;
		return json.dumps(answer_);